# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=flameshot
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=12.1.0
pkgrel=2
pkgdesc="Powerful yet simple to use screenshot software (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://flameshot.org/'
msys2_repository_url="https://github.com/flameshot-org/flameshot"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             'git')
source=(
  "git+$msys2_repository_url#tag=v$pkgver"
  0001-fix-translation-path.patch
  0002-stringify-version-in-rc.patch
  0003-fix-static-linking-with-widgets.patch
  0004-install-all-files.patch
  0005-enable-command-line-options.patch
)
sha256sums=('108f70340fa3facd440ac9428d590befe225305d00943a920ee5594c0007cb38'
            '2264badcaa81f3980ecf7318adc54f8b2951b9fd10bc2eeaf084732f85ab2f7c'
            '959dff8fc2aa0b6a605928b88a960ad0191476de447f937cdbe2f32266767fce'
            '56e999649a6ff256e70b604df4026b2c0f28468797df3b09a625fb172099d644'
            '0279f28b205c77b437f79a180b86622f9cb02aa2c60b593facb48bd2902f25cb'
            'dad8f92babf43def924e50c697d7cb0102dc99e5e7452f605d3f93e3a4a71a45')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    git apply "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}"

  _apply_patch_with_msg \
    0001-fix-translation-path.patch \
    0002-stringify-version-in-rc.patch \
    0003-fix-static-linking-with-widgets.patch \
    0004-install-all-files.patch \
    0005-enable-command-line-options.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_OPENSSL=ON \
      -Wno-dev \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
